package com.ton.utils;

import java.util.Base64;

public class DbPwdSecretUtil {
    public static String secret(String password) {
        try {
            StringBuilder sb = new StringBuilder(password);
            sb.reverse();
            sb.append(sb.charAt(0));
            sb.deleteCharAt(0);
            sb.append(sb.charAt(0));
            sb.append("A");
            sb.append("a");
            return Base64.getEncoder().encodeToString(sb.toString().getBytes("utf-8"));
        } catch (Exception e) {
            throw new RuntimeException("加密错误", e);
        }
    }

    public static String unSecret(String token) {
        try {
            token = new String(Base64.getDecoder().decode(token), "utf-8");
            StringBuilder sb = new StringBuilder(token);
            sb.deleteCharAt(sb.length() - 1);
            sb.deleteCharAt(sb.length() - 1);
            sb.deleteCharAt(sb.length() - 1);
            sb.insert(0, sb.charAt(sb.length() - 1));
            sb.deleteCharAt(sb.length() - 1);
            sb.reverse();
            return sb.toString();
        } catch (Exception e) {
            throw new RuntimeException("解密错误", e);
        }
    }

    public static void main(String[] args) {
        String sec = secret("12345_QWERT_zxcvb_12345");
        System.out.println(sec);
        System.out.println(unSecret(sec));
    }
}
